home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gxccache.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  9KB  |  281 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxccache.c */
  20. /* Fast case character cache routines for Ghostscript library */
  21. #include "gx.h"
  22. #include "gpcheck.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gzstate.h"
  27. #include "gzdevice.h"            /* requires gsstate.h */
  28. #include "gzcolor.h"
  29. #include "gzpath.h"
  30. #include "gxcpath.h"
  31. #include "gxdevmem.h"
  32. #include "gxchar.h"
  33. #include "gxcache.h"
  34. #include "gxfont.h"
  35. #include "gxfdir.h"
  36. #include "gxxfont.h"
  37. #include "gscspace.h"            /* for gsimage.h */
  38. #include "gsimage.h"
  39.  
  40. /* Import one routine from gxccman.c */
  41. extern cached_fm_pair *gx_add_fm_pair(P4(gs_font_dir *,
  42.   gs_font *, const gs_uid *, const gs_state *));
  43.  
  44. /* Look up, and if necessary add, a font/matrix pair in the cache */
  45. cached_fm_pair *
  46. gx_lookup_fm_pair(register const gs_state *pgs)
  47. {    float    mxx = pgs->char_tm.xx, mxy = pgs->char_tm.xy,
  48.         myx = pgs->char_tm.yx, myy = pgs->char_tm.yy;
  49.     gs_font *font = pgs->font;
  50.     register gs_font_dir *dir = font->dir;
  51.     register cached_fm_pair *pair =
  52.         dir->fmcache.mdata + dir->fmcache.mnext;
  53.     int count = dir->fmcache.mmax;
  54.     gs_uid uid;
  55.     if ( font->FontType == ft_composite )
  56.     {    uid_set_invalid(&uid);
  57.     }
  58.     else
  59.     {    uid = font->data.base.UID;
  60.         if ( uid_is_valid(&uid) )
  61.             font = 0;
  62.     }
  63.     while ( count-- )
  64.     {    if ( pair == dir->fmcache.mdata )
  65.             pair += dir->fmcache.mmax;
  66.         pair--;
  67.         /* We either have a non-zero font and an invalid UID, */
  68.         /* or a zero font and a valid UID: */
  69.         if (    (font != 0 ? pair->font == font :
  70.              uid_equal(&pair->UID, &uid)) &&
  71.             pair->mxx == mxx && pair->mxy == mxy &&
  72.             pair->myx == myx && pair->myy == myy
  73.            )
  74.         {    if ( pair->font == 0 )
  75.                 pair->font = pgs->font;
  76.             return pair;
  77.         }
  78.     }
  79.     return gx_add_fm_pair(dir, pgs->font, &uid, pgs);
  80. }
  81.  
  82. /* Look up a glyph in the cache. */
  83. /* Return the cached_char or 0. */
  84. cached_char *
  85. gx_lookup_cached_char(const gs_state *pgs, const cached_fm_pair *pair,
  86.   gs_glyph glyph, int wmode)
  87. {    gs_font_dir *dir = pgs->font->dir;
  88.     register cached_char *cc = *chars_head(dir, glyph, pair);
  89.     while ( cc != 0 )
  90.     {    if ( cc->code == glyph && cc->head.pair == pair && cc->wmode == wmode )
  91.         {    if_debug3('K', "[K]found 0x%lx for glyph=0x%lx, wmode=%d\n",
  92.                   (ulong)cc, (ulong)glyph, wmode);
  93.             return cc;
  94.         }
  95.         cc = cc->next;
  96.     }
  97.     if_debug2('K', "[K]not found: glyph=0x%lx, wmode=%d\n",
  98.           (ulong)glyph, wmode);
  99.     return 0;
  100. }
  101.  
  102. /* Look up a character in an external font. */
  103. /* Return the cached_char or 0. */
  104. cached_char *
  105. gx_lookup_xfont_char(const gs_state *pgs, cached_fm_pair *pair,
  106.   gs_char chr, gs_glyph glyph, gs_proc_glyph_name_t name_proc, int wmode)
  107. {    gs_font *font = pair->font;
  108.     gx_xfont *xf;
  109.     gx_xglyph xg;
  110.     gs_int_point wxy;
  111.     gs_int_rect bbox;
  112.     cached_char *cc;
  113.     if ( font == 0 )
  114.         return NULL;
  115.     if ( !pair->xfont_tried )
  116.     {    /* Look for an xfont now. */
  117.         gx_lookup_xfont(pgs, pair,
  118.                 font->data.base.nearest_encoding_index);
  119.         pair->xfont_tried = 1;
  120.     }
  121.     xf = pair->xfont;
  122.     if ( xf == 0 )
  123.         return NULL;
  124.     /***** The following is wrong.  We should only use the nearest *****/
  125.     /***** registered encoding if the character is really the same. *****/
  126.     xg = (*xf->common.procs->char_xglyph)(xf, chr, font->data.base.nearest_encoding_index, glyph, name_proc);
  127.     if ( xg == gx_no_xglyph )
  128.         return NULL;
  129.     if ( (*xf->common.procs->char_metrics)(xf, xg, wmode, &wxy, &bbox) < 0 )
  130.         return NULL;
  131.     cc = gx_alloc_char_bits(font->dir, NULL, bbox.q.x - bbox.p.x,
  132.                 bbox.q.y - bbox.p.y);
  133.     if ( cc == 0 )
  134.         return NULL;
  135.     /* Success.  Make the cache entry. */
  136.     cc->code = glyph;
  137.     cc->wmode = wmode;
  138.     cc->xglyph = xg;
  139.     cc->wxy.x = int2fixed(wxy.x);
  140.     cc->wxy.y = int2fixed(wxy.y);
  141.     cc->offset.x = int2fixed(-bbox.p.x);
  142.     cc->offset.y = int2fixed(-bbox.p.y);
  143.     if_debug5('k', "[k]xfont %s char %d/0x%x#0x%lx=>0x%lx\n",
  144.           font->font_name.chars,
  145.           font->data.base.nearest_encoding_index, (int)chr,
  146.           (ulong)glyph, (ulong)xg);
  147.     if_debug6('k', "     wxy=(%d,%d) bbox=(%d,%d),(%d,%d)\n",
  148.           wxy.x, wxy.y, bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y);
  149.     gx_add_cached_char(font->dir, NULL, cc, pair, 1);
  150.     return cc;
  151. }
  152.  
  153. /* Copy a cached character to the screen. */
  154. /* Assume the caller has already done gx_color_load. */
  155. /* Return 0 if OK, 1 if we couldn't do the operation but no error */
  156. /* occurred, or a negative error code. */
  157. int
  158. gx_image_cached_char(register gs_show_enum *penum, register cached_char *cc)
  159. {    register gs_state *pgs = penum->pgs;
  160.     int x, y, w, h;
  161.     int code;
  162.     gs_fixed_point pt;
  163.     gx_device *dev = pgs->device->info;
  164.     gx_device_clip cdev;
  165.     gx_xfont *xf;
  166. top:    code = gx_path_current_point_inline(pgs->path, &pt);
  167.     if ( code < 0 ) return code;
  168.     /* If the character doesn't lie entirely within the */
  169.     /* quick-check clipping rectangle, we have to */
  170.     /* set up an intermediate clipping device. */
  171.     pt.x -= cc->offset.x;
  172.     x = fixed2int_var_rounded(pt.x) + penum->ftx;
  173.     pt.y -= cc->offset.y;
  174.     y = fixed2int_var_rounded(pt.y) + penum->fty;
  175.     w = cc->width;
  176.     h = cc->height;
  177. #ifdef DEBUG
  178.     if ( gs_debug['K'] )
  179.     {    if ( cc_has_bits(cc) )
  180.             debug_dump_bytes(cc_bits(cc),
  181.                      cc_bits(cc) + cc->raster * h,
  182.                      "[K]bits");
  183.         else
  184.             dputs("[K]no bits\n");
  185.         dprintf3("[K]copying 0x%lx, offset=(%g,%g)\n", (ulong)cc,
  186.              fixed2float(-cc->offset.x),
  187.              fixed2float(-cc->offset.y));
  188.         dprintf6("   at (%g,%g)+(%d,%d)->(%d,%d)\n",
  189.              fixed2float(pt.x), fixed2float(pt.y),
  190.              penum->ftx, penum->fty, x, y);
  191.     }
  192. #endif
  193.     if (    (x < penum->ibox.p.x || x + w > penum->ibox.q.x ||
  194.          y < penum->ibox.p.y || y + h > penum->ibox.q.y) &&
  195.         dev != (gx_device *)&cdev    /* might be 2nd time around */
  196.        )
  197.     {    /* Check for the character falling entirely outside */
  198.         /* the clipping region. */
  199.         if ( x >= penum->obox.q.x || x + w <= penum->obox.p.x ||
  200.              y >= penum->obox.q.y || y + h <= penum->obox.p.y
  201.            )
  202.             return 0;        /* nothing to do */
  203.         cdev = gs_clip_device;
  204.         cdev.target = dev;
  205.         cdev.list = pgs->clip_path->list;
  206.         dev = (gx_device *)&cdev;
  207.         (*dev->procs->open_device)(dev);
  208.         if_debug0('K', "[K](clipping)\n");
  209.     }
  210.     /* If an xfont can render this character, use it. */
  211.     if ( cc->xglyph != gx_no_xglyph && (xf = cc->head.pair->xfont) != 0 )
  212.     {    int cx = x + fixed2int(cc->offset.x);
  213.         int cy = y + fixed2int(cc->offset.y);
  214.         if ( color_is_pure(pgs->dev_color) )
  215.         {    code = (*xf->common.procs->render_char)(xf,
  216.                 cc->xglyph, dev, cx, cy,
  217.                 pgs->dev_color->color1, 0);
  218.             if ( code >= 0 )
  219.                 return_check_interrupt(0);
  220.         }
  221.         /* Can't render directly.  If we don't have a bitmap yet, */
  222.         /* get it from the xfont now. */
  223.         if ( !cc_has_bits(cc) )
  224.         {    gx_device_memory mdev;
  225.             mdev = mem_mono_device;
  226.             mdev.target = dev;
  227.             gx_open_cache_device(&mdev, cc);
  228.             code = (*xf->common.procs->render_char)(xf,
  229.                 cc->xglyph, (gx_device *)&mdev,
  230.                 cx - x, cy - y,
  231.                 (gx_color_index)1, 1);
  232.             if ( code < 0 )
  233.                 return_check_interrupt(1);
  234.             gx_add_char_bits(cc->head.pair->font->dir,
  235.                      &mdev, cc, 1);
  236.             /* gx_add_char_bits may change width, height, */
  237.             /* raster, and/or offset.  It's easiest to */
  238.             /* start over from the top. */
  239.             goto top;
  240.         }
  241.     }
  242.     /* No xfont.  Render from the cached bits. */
  243.     if ( color_is_pure(pgs->dev_color) )
  244.     {    code = (*dev->procs->copy_mono)
  245.             (dev, cc_bits(cc), 0, cc->raster, cc->id,
  246.             x, y, w, h,
  247.             gx_no_color_index, pgs->dev_color->color1);
  248.     }
  249.     else
  250.     {    /* Use imagemask to render the character. */
  251.         gs_image_enum *pie =
  252.             (gs_image_enum *)gs_malloc(1,
  253.                 gs_image_enum_sizeof,
  254.                 "image_char(image_enum)");
  255.         gs_matrix mat;
  256.         int iy;
  257.         if ( pie == 0 )
  258.             return_error(gs_error_VMerror);
  259.         /* Make a matrix that will place the image */
  260.         /* at (x,y) with no transformation. */
  261.         gs_make_translation((floatp)-x, (floatp)-y, &mat);
  262.         gs_matrix_multiply(&ctm_only(pgs), &mat, &mat);
  263.         code = gs_imagemask_init(pie, pgs, w, h, 0, &mat, 0);
  264.         switch ( code )
  265.         {
  266.         case 1:            /* empty image */
  267.             code = 0;
  268.         default:
  269.             break;
  270.         case 0:
  271.             for ( iy = 0; iy < h && code >= 0; iy++ )
  272.               code = gs_image_next(pie, cc_bits(cc) + iy *
  273.                 cc->raster, (w + 7) >> 3);
  274.             gs_image_cleanup(pie);
  275.         }
  276.         gs_free((char *)pie, 1, gs_image_enum_sizeof,
  277.             "image_char(image_enum)");
  278.     }
  279.     return_check_interrupt(code);
  280. }
  281.